import React, { useState } from 'react';
import { Form, Button, Input, Modal } from 'antd';

import {EnvCreateParam} from '../data.d';

export interface CreateFormProps {
  onCancel: (flag?: boolean, formVals?: EnvCreateParam) => void;
  onSubmit: (value: EnvCreateParam) => void;
  createEnvModalVisible: boolean;
}
const FormItem = Form.Item;

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const CreateForm: React.FC<CreateFormProps> = (props) => {
  const [formValues, setFormValues] = useState<EnvCreateParam>({
    envName: "",
  });

  const [form] = Form.useForm();

  const {
    onSubmit: handleCreate,
    onCancel: handleCreateModalVisible,
    createEnvModalVisible,
  } = props;

  const handleRealCreate = async () => {
    const fieldsValue = await form.validateFields();
    setFormValues({ ...formValues, ...fieldsValue });
    handleCreate({ ...formValues, ...fieldsValue });
  }

  const renderFooter = () => {
    return (
      <>
        <Button onClick={() => handleCreateModalVisible(false)}>取消</Button>
        <Button type="primary" onClick={() => handleRealCreate()}>创建</Button>
      </>
    );
  };

  return (
    <Modal
      width={640}
      bodyStyle={{ padding: '32px 40px 48px' }}
      destroyOnClose
      title="创建新环境"
      visible={createEnvModalVisible}
      footer={renderFooter()}
      onCancel={() => handleCreateModalVisible()}
    >
      <Form
        {...formLayout}
        form={form}
        initialValues={{
          envName: formValues.envName,
        }}
      >
        <FormItem
          name="envName"
          label="创建环境"
        >
          <Input placeholder="请输入" />
        </FormItem>
      </Form>
    </Modal>
  );
};

export default CreateForm;
